package com.kuaimadi;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class SimpleGenerator {

    private static final String DBURL = "localhost:3306";
    private static final String DATABASE_NAME = "mall";//数据库名

    private static final String classDriver = "com.mysql.cj.jdbc.Driver";//数据库驱动
    private static final String DATA_SOURCE_USER_NAME  = "root";
    private static final String DATA_SOURCE_PASSWORD  = "123456";

    private static final String LOCATION_PACKAGE  = "com.kuaimadi";


    private static final Map<String,String> DATABASENAMETOJAVANAMEMAP =new LinkedHashMap<>();

    public static void main(String[] args) {

        DATABASENAMETOJAVANAMEMAP.put("orders","Orders");
//        DATABASENAMETOJAVANAMEMAP.put("items_comments","ItemsComments");


        Set<Map.Entry<String, String>> entries = DATABASENAMETOJAVANAMEMAP.entrySet();
        for (Map.Entry<String, String>  dataToJavaMap : entries){

            String dataName = dataToJavaMap.getKey();
            String javaName = dataToJavaMap.getValue();
            generatorCode(dataName,javaName);
        }



    }

    static  void generatorCode(String database_name,String javaname){
        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setFileOverride(true);
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/mybatis-plus-genrator/src/main/java");
        globalConfig.setAuthor("gym");
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(true);
        // 生成代码的文件名
        globalConfig.setEntityName(javaname); //model
        globalConfig.setMapperName(javaname+"Mapper"); //dao
        globalConfig.setXmlName(javaname+"Mapper");//xml
        globalConfig.setServiceName(javaname+"Service");
        globalConfig.setServiceImplName(javaname+"ServiceImpl");
        globalConfig.setBaseResultMap(true);

        globalConfig.setDateType(DateType.ONLY_DATE);//时间类型的对应策略
//        globalConfig.setIdType(IdType.ASSIGN_ID);
        generator.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl("jdbc:mysql://"+DBURL+"/" + DATABASE_NAME
                + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true");
        dataSourceConfig.setDriverName(classDriver);
        dataSourceConfig.setUsername(DATA_SOURCE_USER_NAME);
        dataSourceConfig.setPassword(DATA_SOURCE_PASSWORD);
        generator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(LOCATION_PACKAGE);
        packageConfig.setEntity("pojo");//生成的实体类的子包名
        packageConfig.setMapper("mapper");//生成的dao层的子包名
        packageConfig.setXml("mapper.xml");//生成的mapper.xml文件层的子包名
        generator.setPackageInfo(packageConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setInclude(database_name); // 设置要生成的数据库中的表名
        strategyConfig.setControllerMappingHyphenStyle(true);//去除生成controller层的getmapping url的连字符
        /*strategyConfig.setTablePrefix(packageConfig.getModuleName() + "_");
        这行代码的作用是，在生成数据库表对应的实体类、Mapper接口等代码时，会将这个模块名称作为表的前缀，以避免不同模块的表发生命名冲突。
        例如，如果模块名称为"user"，那么生成的表名会是"user_table"，而不是简单的"table"。
        哈哈这里为空 没用
         */
        strategyConfig.setTablePrefix(packageConfig.getModuleName() + "_");
        // Boolean类型字段是否移除is前缀处理
        strategyConfig.setEntityBooleanColumnRemoveIsPrefix(true);
        strategyConfig.setRestControllerStyle(true);
        generator.setStrategy(strategyConfig);

        // 自定义配置，可以在这里进行其他自定义设置，例如模板引擎配置、文件输出等
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                // do nothing
            }
        };
        generator.setCfg(injectionConfig);

        // 执行生成
        generator.execute();

    }

}
